// https://www.luogu.com.cn/problem/P2014
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
using ll = long long;
const int inf = 0x3f3f3f3f;
#define rf(i, n) for (int i = 1; i <= (n); ++i)
int rad();  // read int
const int max_size = 5 + 3e2;
const int maxn = 5 + 3e2;

int n, m;
int a[maxn];
vector<int> adj[maxn];
int f[maxn][max_size];

void dfs(int now) {
    f[now][1] = a[now];
    for (auto &p : adj[now]) {
        dfs(p);
        for (int j = m + 1; j >= 1; --j) {  // 0 号节点，总共 m + 1 节课
            for (int k = 0; k < j; ++k) {   // p 子树能不选，但是 now 必须选，所以 k < j
                f[now][j] = max(f[now][j], f[now][j - k] + f[p][k]);
            }
        }
    }
}

int main() {
    n = rad(), m = rad();
    rf(i, n) {
        adj[rad()].push_back(i);
        a[i] = rad();
    }
    dfs(0);
    printf("%d\n", f[0][m + 1]);
}
/*
0: 2, 3
1:
2: 1, 4, 7
3:
4:
5:
6:
7: 5, 6,
*/

int rad() {
    int back = 0, ch = 0, posi = 0;
    for (; ch < '0' || ch > '9'; ch = getchar())
        posi = ch ^ '-';
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        back = (back << 1) + (back << 3) + (ch & 0xf);
    return posi ? back : -back;
}